package com.hp.common.validator;

import com.hp.common.base.BaseMapper;
import com.hp.common.base.CommonError;
import com.hp.common.base.constant.GlobalErrEnum;
import com.hp.common.exception.BizException;
import tk.mybatis.mapper.entity.Condition;

public class ValidatorTool {

    public ValidatorTool() {
    }

   public static boolean checkUnique(BaseMapper<?> baseMapper,Class<?> clazz,String property,Object value){
       Condition condition = new Condition(clazz);
       condition.createCriteria()
               .andEqualTo(property, value);
       int count = baseMapper.selectCountByCondition(condition);
       if(count > 0){
           throw new BizException(new CommonError() {
               @Override
               public int getValue() {
                   return GlobalErrEnum.GL99990100.getValue();
               }

               @Override
               public String getName() {
                   return property + "字段唯一，请更换别的值";
               }
           });
       }
       return true;
   }
}
